import 'dart:ui' as ui show window;

import 'package:flutter/material.dart';

class SizeFill {
  final double standardSize = 750;
  final double standardHeight = 1624;

  static final SizeFill _singleton = SizeFill();

  static SizeFill getInstance() {
    _singleton._init();
    return _singleton;
  }

  MediaQueryData? _mediaQueryData;

  static double? physicalWidth; //屏幕物理宽度
  static double? physicalHeight; //屏幕物理高度
  static double? screenWidth; //屏幕宽度
  static late double screenHeight; //屏幕高度

  static late double dpr; //屏幕比例
  static double? statusHeight; //状态栏高度
  static double? safeHeight; //安全区高度

  //屏幕宽比例
  static double? rpx;
  static late double px;

  //屏幕高比例
  static late double heightRpx;
  static late double heightPx;

  _init() {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    if (_mediaQueryData != mediaQuery) {
      _mediaQueryData = mediaQuery;
      dpr = mediaQuery.devicePixelRatio;

      physicalWidth = mediaQuery.size.width;
      physicalHeight = mediaQuery.size.height;

      //拿到手机屏幕大小（逻辑分辨率)
      screenWidth = mediaQuery.size.width ;// dpr;
      screenHeight = mediaQuery.size.height;// / dpr;
      rpx = screenWidth! / standardSize;
      px = screenWidth! / standardSize * 2;
      heightRpx = screenHeight / standardHeight;
      heightPx = screenHeight / standardHeight * 2;
      statusHeight = mediaQuery.padding.top;///dpr;
      safeHeight = mediaQuery.padding.bottom;///dpr;
    }
  }

  static double setRpx(double size) {
    return size * rpx!;
  }

  static double setRx(double size) {
    return px * size;
  }

  static double setHeightRpx(double size) {
    print(screenWidth);
    print(SizeFill.rpx);
    return size * heightRpx;
  }

  static double setHeightRx(double size) {
    return size * heightPx;
  }
}
